package com.fanpan26.thread.print;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author fanpan26
 */
public class PrinterThree {

    private static final String[] strs = new String[]{"1","2","3"};

    private AtomicInteger index = new AtomicInteger();


    public void print(int target) throws InterruptedException {

        synchronized (this) {
            while (index.get() % 3 != target) {
                wait();
            }
            System.out.println(strs[target]);
            index.getAndIncrement();
            notifyAll();
        }
    }


    public static void main(String[] args) throws InterruptedException{
        PrinterThree three = new PrinterThree();

        Thread threadA = new Thread(new ThreadPrintA(three));
        Thread threadB = new Thread(new ThreadPrintB(three));
        Thread threadC = new Thread(new ThreadPrintC(three));

        threadA.start();
        threadB.start();
        threadC.start();
        threadA.join();
        threadB.join();
        threadC.join();
        System.out.println("finished");
    }

}
